package com.example.myapplication;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

public class MyItem extends LinearLayout {
    private static final String TAG = "MyItem";
    public MyItem(Context context) {
        super(context);
    }

    public MyItem(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //获取当前类型控件中的自定义属性数组
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.MyItem);
        String title =typedArray.getString(R.styleable.MyItem_textTitle);
        String hint =typedArray.getString(R.styleable.MyItem_textHint);
        Drawable iconHint= typedArray.getDrawable(R.styleable.MyItem_iconHint);
        Drawable iconTitle= typedArray.getDrawable(R.styleable.MyItem_iconTitle);

        typedArray.recycle();
        Log.e(TAG, "MyItem: "+title );
        LayoutInflater.from(context).inflate(R.layout.my_item_layout,this);
        TextView tvTitle =findViewById(R.id.tv_title);
        tvTitle.setText(title);
        TextView tvHint =findViewById(R.id.tv_hint);
        tvHint.setText(hint);
        ImageView ivTitle =this.findViewById(R.id.iv_title);
        ivTitle.setImageDrawable(iconTitle);
        ImageView ivHint =this.findViewById(R.id.iv_hint);
        ivHint.setImageDrawable(iconHint);

    }
}
